package com.qf.shop.service.impl;

import com.qf.shop.dao.Impl.UserDaoImpl;
import com.qf.shop.dao.UserDao;
import com.qf.shop.entity.User;
import com.qf.shop.service.UserService;
import com.qf.shop.utils.DruidUtils;


public class UserServiceImpl implements UserService {
    private UserDao  userDao=  new UserDaoImpl();
    @Override
    public User checkUsername(String username) {
        User user = null;
        try {
            DruidUtils.begin();
            user = userDao.selectByUsername(username);
            System.out.println("user22222 = " + user);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int register(User user) {
        int result = 0;
        try {
            DruidUtils.begin();
            result=userDao.insertUser(user);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }

        return  result;
    }

    @Override
    public int activeUser(String email, String code) {
        int result = 0;
        try {
            DruidUtils.begin();
            result= userDao.update(email,code);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public User loginUser(String username, String password) {
        User user = null;
        try {
            DruidUtils.begin();
            User temp = userDao.selectByUsername(username);
            if(temp!=null){
                if(temp.getPassword().equals(password)){
                    user = temp;
                }
            }
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return user;
    }
}
